//
//  THJProductManageVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/12/12.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJProductManageVC.h"
#import "THJProductManageCell.h"
#import "ZBNetworking.h"
#import "THJPurchaseVC.h"
@interface THJProductManageVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray * dataArr;
    UITableView * _tableview;
}
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@end

@implementation THJProductManageVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"产品管理"];
    }else{
        [self setNavBarWithTitle:@"产品管理"];
    }
    self.topSpace.constant = NavbarHeight;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    // Do any additional setup after loading the view from its nib.
}
-(void)getData{
    __weak typeof(self) weekself = self;
    NSString * userid = [[NSUserDefaults standardUserDefaults] objectForKey:@"userid"];
    NSString * urlstr = [NSString stringWithFormat:@"%@index.php?f=sell_5&r=sell_5&userid=%@",THJUrlDomain,userid];
    NSLog(@"产品管理url-%@",urlstr);
    [MBProgressHUD showMessage:@"" toView:self.view];
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString=urlstr;
        request.apiType=ZBRequestTypeRefresh;
    }  success:^(id responseObject,apiType type){
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        [MBProgressHUD hideHUDForView:weekself.view animated:YES];
        if([receiveData[@"status"] isEqual:@"200"]){
            dataArr = receiveData[@"data"];
            _tableview =   [weekself setTableviewWithFrame:CGRectMake(0, 110, ScreenWidth, ScreenHeight - NavbarHeight - 25) RegisterNibNameArr:@[@"THJProductManageCell"] ReuseIdentifierArr:@[@"cell"] RowHeight:75 BgColor:SingleColor(whiteColor) IsLoadMore:NO];
        }
    } failed:^(NSError *error){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"error-%@",error);
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dic = dataArr[indexPath.row];
    THJProductManageCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLab.text = dic[@"title"];
    [cell.iconImgV sd_setImageWithURL:[NSURL URLWithString:dic[@"thumb"]] placeholderImage:kGetImage(@"占位图")];
    [cell.btn1 addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.btn1.tag = indexPath.row+1000;
    [cell.btn2 addTarget:self action:@selector(xiugaiAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.btn2.tag = indexPath.row+2000;
    return cell;
}
-(void)xiugaiAction:(UIButton*)btn{
    NSLog(@"修改-%ld",btn.tag);
}
-(void)deleteAction:(UIButton*)btn{
    NSLog(@"删除-%ld",btn.tag);
    NSInteger index = btn.tag - 1000;
    NSDictionary * dic = dataArr[index];
    NSString * itemid = dic[@"itemid"];
    
   
    NSString * deleteUrl = [NSString stringWithFormat:@"%@index.php?f=My_sell_5&r=My_sell_5&m=del&itemid=%@",THJUrlDomain,itemid];
    NSString * token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    [MBProgressHUD showMessage:@"" toView:self.view];
     __weak typeof(self) weakself = self;
    [THJNetWork postDataFromUrl:deleteUrl parameters:@{@"itemid":itemid} Token:token callback:^(NSDictionary * receiveData, NSError *error) {
        [weakself HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
        if([receiveData[@"status"] isEqual:@"200"]){
            [MBProgressHUD hideHUDForView:weakself.view animated:YES];
            NSLog(@"删除反馈-%@",receiveData);
            [dataArr removeObjectAtIndex:index];
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                //通知主线程刷新
                dispatch_async(dispatch_get_main_queue(), ^{
                    //回调或者说是通知主线程刷新，
                    [_tableview reloadData];
                });
                
            });
            
        }
    }];
}
/**
 点击加号
 */
- (IBAction)plusBtnAction:(id)sender {
    [self presentViewController:[THJPurchaseVC new] animated:YES completion:nil];
}
@end
